"use strict";
const common_vendor = require("../../../../../common/vendor.js");
const pages_TUIKit_TUICore_server_IComponentServer = require("../IComponentServer.js");
const pages_TUIKit_TUICore_store_index = require("../../store/index.js");
class TUIChatServer extends pages_TUIKit_TUICore_server_IComponentServer.IComponentServer {
  constructor(TUICore) {
    super();
    this.currentStore = {};
    this.store = pages_TUIKit_TUICore_store_index.store.state.timStore;
    this.TUICore = common_vendor.index.$TUIKit;
    this.bindTIMEvent();
    this.updateStore();
  }
  /**
   * 组件销毁
   */
  destroyed() {
    this.unbindTIMEvent();
  }
  /**
   * 数据监听回调
   *
   * @param {any} newValue 新数据
   * @param {any} oldValue 旧数据
   *
   */
  updateStore() {
    this.getMessageList({
      conversationID: this.store.conversationID,
      count: 15
    });
    pages_TUIKit_TUICore_store_index.store.commit("timStore/resetChat");
  }
  /**
   * /////////////////////////////////////////////////////////////////////////////////
   * //
   * //                                    TIM 事件监听注册接口
   * //
   * /////////////////////////////////////////////////////////////////////////////////
   */
  bindTIMEvent() {
    this.TUICore.on(
      common_vendor.index.$TIM.EVENT.MESSAGE_RECEIVED,
      this.handleMessageReceived,
      this
    );
    this.TUICore.on(
      common_vendor.index.$TIM.EVENT.MESSAGE_MODIFIED,
      this.handleMessageModified,
      this
    );
    this.TUICore.on(
      common_vendor.index.$TIM.EVENT.MESSAGE_REVOKED,
      this.handleMessageRevoked,
      this
    );
    this.TUICore.on(
      common_vendor.index.$TIM.EVENT.MESSAGE_READ_BY_PEER,
      this.handleMessageReadByPeer,
      this
    );
    this.TUICore.on(
      common_vendor.index.$TIM.EVENT.GROUP_LIST_UPDATED,
      this.handleGroupListUpdated,
      this
    );
  }
  unbindTIMEvent() {
    this.TUICore.off(
      common_vendor.index.$TIM.EVENT.MESSAGE_RECEIVED,
      this.handleMessageReceived
    );
    this.TUICore.off(
      common_vendor.index.$TIM.EVENT.MESSAGE_MODIFIED,
      this.handleMessageModified
    );
    this.TUICore.off(common_vendor.index.$TIM.EVENT.MESSAGE_REVOKED, this.handleMessageRevoked);
    this.TUICore.off(
      common_vendor.index.$TIM.EVENT.MESSAGE_READ_BY_PEER,
      this.handleMessageReadByPeer
    );
    this.TUICore.off(
      common_vendor.index.$TIM.EVENT.GROUP_LIST_UPDATED,
      this.handleGroupListUpdated,
      this
    );
  }
  handleMessageReceived(event) {
    event.data.forEach((item) => {
      if (item.conversationID === this.store.conversationID) {
        common_vendor.index.$TUIKit.TUIConversationServer.setMessageRead(item.conversationID);
        const messageList = [...this.store.messageList, item];
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
      }
    });
  }
  handleMessageModified(event) {
    const middleData = this.store.messageList;
    this.store.messageList = [];
    this.store.messageList = middleData;
  }
  handleMessageRevoked(event) {
  }
  async handleMessageReadByPeer(event) {
    const middleData = this.store.messageList;
    await pages_TUIKit_TUICore_store_index.store.commit("timStore/resetChat");
    await pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", middleData);
  }
  handleGroupListUpdated(event) {
    event == null ? void 0 : event.data.map((item) => {
      var _a, _b, _c;
      if ((item == null ? void 0 : item.groupID) === ((_c = (_b = (_a = this == null ? void 0 : this.store) == null ? void 0 : _a.conversation) == null ? void 0 : _b.groupProfile) == null ? void 0 : _c.groupID)) {
        this.store.conversation.groupProfile = item;
        const midden = this.store.conversation;
        this.store.conversation = {};
        this.store.conversation = midden;
      }
      return item;
    });
  }
  /**
   * /////////////////////////////////////////////////////////////////////////////////
   * //
   * //                                 处理 TIM 接口参数及回调
   * //
   * /////////////////////////////////////////////////////////////////////////////////
   */
  /**
   * 创建消息生成参数
   *
   * @param {Object} content 消息体
   * @param {String} type 消息类型 text: 文本类型 file: 文件类型 face: 表情 location: 地址 custom: 自定义 merger: 合并 forward: 转发
   * @param {Callback} callback 回调函数
   * @param {any} to 发送的对象
   * @returns {options} 消息参数
   */
  handleMessageOptions(content, type, callback, to) {
    var _a, _b, _c, _d, _e, _f;
    const options = {
      to: "",
      conversationType: (to == null ? void 0 : to.type) || this.store.conversation.type,
      payload: content,
      needReadReceipt: true
    };
    if (type === "file" && callback) {
      options.onProgress = callback;
    }
    switch (options.conversationType) {
      case common_vendor.index.$TIM.TYPES.CONV_C2C:
        options.to = ((_a = to == null ? void 0 : to.userProfile) == null ? void 0 : _a.userID) || ((_c = (_b = this.store.conversation) == null ? void 0 : _b.userProfile) == null ? void 0 : _c.userID) || "";
        break;
      case common_vendor.index.$TIM.TYPES.CONV_GROUP:
        options.to = ((_d = to == null ? void 0 : to.groupProfile) == null ? void 0 : _d.groupID) || ((_f = (_e = this.store.conversation) == null ? void 0 : _e.groupProfile) == null ? void 0 : _f.groupID) || "";
        break;
    }
    return options;
  }
  /**
   * 处理异步函数
   *
   * @param {callback} callback 回调函数
   * @returns {Promise} 返回异步函数
   */
  handlePromiseCallback(callback) {
    return new Promise((resolve, reject) => {
    });
  }
  /**
   * 文件上传进度函数处理
   *
   * @param {number} progress 文件上传进度 1表示完成
   * @param {message} message 文件消息
   */
  handleUploadProgress(progress, message) {
    this.store.messageList.map((item) => {
      if (item.ID === message.ID) {
        item.progress = progress;
      }
      return item;
    });
  }
  /**
   * /////////////////////////////////////////////////////////////////////////////////
   * //
   * //                                 对外方法
   * //
   * /////////////////////////////////////////////////////////////////////////////////
   */
  // /**
  //  * 发送文本消息
  //  *
  //  * @param {any} text 发送的消息
  //  * @returns {Promise}
  //  */
  /**
   * 发送文本消息
   *
   * @param {any} text 发送的消息
   * @returns {Promise}
   */
  sendTextMessage(text) {
    return new Promise(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions({ text }, "text");
        const message = this.TUICore.createTextMessage(options);
        const messageList = [...this.store.messageList, message];
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
        const imResponse = await this.TUICore.sendMessage(message);
        this.store.messageList = this.store.messageList.map((item) => {
          if (item.ID === imResponse.data.message.ID) {
            return imResponse.data.message;
          }
          return item;
        });
        resolve(imResponse);
      } catch (error) {
        reject(error);
        const middleData = this.store.messageList;
        pages_TUIKit_TUICore_store_index.store.commit("timStore/resetChat");
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", middleData);
      }
    });
  }
  /**
   * 发送表情消息
   *
   * @param {Object} data 消息内容
   * @param {Number} data.index 表情索引
   * @param {String} data.data 额外数据
   * @returns {Promise}
   */
  sendFaceMessage(data) {
    return new Promise(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions(data, "face");
        const message = this.TUICore.createFaceMessage(options);
        const messageList = [...this.store.messageList, message];
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
        const imResponse = await this.TUICore.sendMessage(message);
        this.store.messageList = this.store.messageList.map((item) => {
          if (item.ID === imResponse.data.message.ID) {
            return imResponse.data.message;
          }
          return item;
        });
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 发送图片消息
   *
   * @param {res} res 图片文件
   * @param {image} 图片尺寸
   * @returns {Promise}
   */
  sendImageMessage(res, image) {
    return new Promise(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions({ file: res }, "file");
        const message = this.TUICore.createImageMessage(options);
        message.payload.imageInfoArray[1].height = image.height;
        message.payload.imageInfoArray[1].width = image.width;
        const messageList = [...this.store.messageList, message];
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
        const imResponse = await this.TUICore.sendMessage(message);
        this.store.messageList = this.store.messageList.map((item) => {
          if (item.ID === imResponse.data.message.ID) {
            return imResponse.data.message;
          }
          return item;
        });
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 发送视频消息
   *
   * @param {Video} video 图片文件
   * @returns {Promise}
   */
  sendVideoMessage(res, video) {
    return new Promise(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions({ file: res }, "file");
        const message = this.TUICore.createVideoMessage(options);
        const messageList = [...this.store.messageList, message];
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
        const imResponse = await this.TUICore.sendMessage(message);
        this.store.messageList = this.store.messageList.map((item) => {
          if (item.ID === imResponse.data.message.ID) {
            return imResponse.data.message;
          }
          return item;
        });
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 发送语音消息
   *
   * @param {audio} audio 音频文件
   * @returns {Promise}
   */
  sendAudioMessage(audio) {
    return new Promise(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions(
          { file: audio },
          "file",
          (progress) => {
            this.handleUploadProgress(progress, message);
          }
        );
        const message = this.TUICore.createAudioMessage(options);
        const messageList = [...this.store.messageList, message];
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
        const imResponse = await this.TUICore.sendMessage(message);
        console.log("发送音频消息完成", imResponse);
        this.store.messageList = this.store.messageList.map((item) => {
          if (item.ID === imResponse.data.message.ID) {
            return imResponse.data.message;
          }
          return item;
        });
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 发送文件消息
   *
   * @param {File} file 文件
   * @returns {Promise}
   */
  sendFileMessage(file) {
    return this.handlePromiseCallback(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions(
          { file },
          "file",
          (progress) => {
            this.handleUploadProgress(progress, message);
          }
        );
        const message = this.TUICore.createFileMessage(options);
        this.currentStore.messageList.push(message);
        const imResponse = await this.TUICore.sendMessage(message);
        console.log("发送文件消息完成", imResponse);
        this.store.messageList = this.store.messageList.map((item) => {
          if (item.ID === imResponse.data.message.ID) {
            return imResponse.data.message;
          }
          return item;
        });
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 发送自定义消息
   *
   * @param {Object} data 消息内容
   * @param {String} data.data 自定义消息的数据字段
   * @param {String} data.description 自定义消息的说明字段
   * @param {String} data.extension 自定义消息的扩展字段
   * @returns {Promise}
   */
  sendCustomMessage(data) {
    return this.handlePromiseCallback(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions(data, "custom");
        const message = this.TUICore.createCustomMessage(options);
        this.currentStore.messageList.push(message);
        const imResponse = await this.TUICore.sendMessage(message);
        console.log("发送自定义消息完成", imResponse);
        this.store.messageList = this.store.messageList.map((item) => {
          if (item.ID === imResponse.data.message.ID) {
            return imResponse.data.message;
          }
          return item;
        });
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 发送地理位置消息
   *
   * @param {Object} data 消息内容
   * @param {String} data.description 地理位置描述信息
   * @param {Number} data.longitude 经度
   * @param {Number} data.latitude 纬度
   * @returns {Promise}
   */
  sendLocationMessage(data) {
    return this.handlePromiseCallback(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions(data, "location");
        const message = this.TUICore.createLocationMessage(options);
        this.store.messageList.push(message);
        const imResponse = await this.TUICore.sendMessage(message);
        console.log("发送地理位置消息完成", imResponse);
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 转发消息
   *
   * @param {message} message 消息实例
   * @param {any} to 转发的对象
   * @returns {Promise}
   */
  forwardMessage(message, to) {
    return this.handlePromiseCallback(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions(message, "forward", {}, to);
        const imMessage = this.TUICore.createForwardMessage(options);
        const imResponse = await this.TUICore.sendMessage(imMessage);
        if (this.store.conversation.conversationID === imResponse.data.message.conversationID) {
          this.store.messageList.push(imResponse.data.message);
        }
        console.log("消息转发完成", imResponse);
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 发送@ 提醒功能的文本消息
   *
   * @param {Object} data 消息内容
   * @param {String} data.text 文本消息
   * @param {Array} data.atUserList 需要 @ 的用户列表，如果需要 @ALL，请传入 TIM.TYPES.MSG_AT_ALL
   * @returns {message}
   *
   * - 注：此接口仅用于群聊
   */
  sendTextAtMessage(data) {
    return new Promise(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions(data, "text");
        const message = this.TUICore.createTextAtMessage(options);
        this.currentStore.messageList.push(message);
        const imResponse = await this.TUICore.sendMessage(message);
        this.currentStore.messageList = this.currentStore.messageList.map(
          (item) => {
            if (item.ID === imResponse.data.message.ID) {
              return imResponse.data.message;
            }
            return item;
          }
        );
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 发送合并消息
   *
   * @param {Object} data 消息内容
   * @param {Array.<Message>} data.messageList 合并的消息列表
   * @param {String} data.title 合并的标题
   * @param {String} data.abstractList 摘要列表，不同的消息类型可以设置不同的摘要信息
   * @param {String} data.compatibleText 兼容文本
   * @returns {Promise}
   */
  sendMergerMessage(data) {
    return this.handlePromiseCallback(async (resolve, reject) => {
      try {
        const options = this.handleMessageOptions(data, "merger");
        const message = this.TUICore.createMergerMessage(options);
        this.currentStore.messageList.push(message);
        const imResponse = await this.TUICore.sendMessage(message);
        console.log("发送合并消息完成", imResponse);
        this.currentStore.messageList = this.currentStore.messageList.map(
          (item) => {
            if (item.ID === imResponse.data.message.ID) {
              return imResponse.data.message;
            }
            return item;
          }
        );
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 获取 messageList
   *
   * @param {any} options 获取 messageList 参数
   * @param {Boolean} history  是否获取历史消息
   * @returns {Promise}
   */
  async getMessageList(options, history) {
    return new Promise(async (resolve, reject) => {
      try {
        const imResponse = await this.TUICore.getMessageList(options);
        let messageList;
        if (!history) {
          messageList = imResponse.data.messageList;
        } else {
          messageList = [
            ...imResponse.data.messageList,
            ...this.store.messageList
          ];
        }
        this.currentStore.nextReqMessageID = imResponse.data.nextReqMessageID;
        this.currentStore.isCompleted = imResponse.data.isCompleted;
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
        resolve(imResponse.data);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 获取历史消息
   *
   * @returns {Promise}
   */
  async getHistoryMessageList() {
    return new Promise(async (resolve, reject) => {
      try {
        const options = {
          conversationID: this.store.conversation.conversationID,
          nextReqMessageID: this.currentStore.nextReqMessageID,
          count: 15
        };
        let messageList = [];
        if (!this.currentStore.isCompleted) {
          messageList = await this.getMessageList(options, true);
        }
        resolve(messageList);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 消息撤回
   *
   * @param {message} message 消息实例
   * @returns {Promise}
   */
  revokeMessage(message) {
    return new Promise(async (resolve, reject) => {
      try {
        const imResponse = await this.TUICore.revokeMessage(message);
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 重发消息
   *
   * @param {message} message 消息实例
   * @returns {Promise}
   */
  resendMessage(message) {
    return new Promise(async (resolve, reject) => {
      try {
        const imResponse = await this.TUICore.resendMessage(message);
        console.log("消息重发完成", imResponse);
        let messageList = [];
        messageList = this.store.messageList.filter(
          (item) => item.ID !== message.ID
        );
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
  /**
   * 删除消息
   *
   * @param {Array.<message>} messages 消息实例
   * @returns {Promise}
   */
  deleteMessage(messages) {
    return new Promise(async (resolve, reject) => {
      try {
        const imResponse = await this.TUICore.deleteMessage(messages);
        let messageList = [];
        messageList = this.store.messageList.filter(
          (item) => item.ID !== messages.ID
        );
        pages_TUIKit_TUICore_store_index.store.commit("timStore/setMessageList", messageList);
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }
}
exports.TUIChatServer = TUIChatServer;
